package com.xgimi.open.skill.datamanager.domain.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.xgimi.open.skill.datamanager.domain.entity.EntryDO;
import com.xgimi.open.skill.datamanager.domain.mapper.EntryMapper;
import com.xgimi.open.skill.datamanager.domain.service.EntryService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Objects;

/**
 * @author coderzpw.zhang
 * @description 针对表【entry(词条表)】的数据库操作Service实现
 * @createDate 2023-04-10 19:26:26
 */
@Service
public class EntryServiceImpl extends ServiceImpl<EntryMapper, EntryDO>
        implements EntryService {


    /**
     * 根据词库id 批量查询词条信息（支持value模糊查询）
     *
     * @param page
     * @param mapperReq
     * @return
     */
    @Override
    public IPage<EntryDO> pageQuery(IPage<EntryDO> page, EntryDO mapperReq) {
        LambdaQueryWrapper<EntryDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(!Objects.isNull(mapperReq.getWordStockId()), EntryDO::getWordStockId, mapperReq.getWordStockId());
        queryWrapper.like(!StringUtils.isEmpty(mapperReq.getValue()), EntryDO::getValue, mapperReq.getValue());
        queryWrapper.orderByDesc(EntryDO::getCreatedAt);
        return this.page(page, queryWrapper);
    }

    /**
     * 根据词库id，查询词库所有的词条。 若词库id为空则返回空集合
     *
     * @param wordStockId
     * @return
     */
    @Override
    public List<EntryDO> listByWordStockId(Long wordStockId) {
        if (Objects.isNull(wordStockId)) {
            return Lists.newArrayList();
        }
        LambdaQueryWrapper<EntryDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EntryDO::getWordStockId, wordStockId);
        return this.list(queryWrapper);
    }

    /**
     * 根据value查询某个词条
     *
     * @param newValue
     * @return
     */
    @Override
    public EntryDO getByValue(String newValue) {
        LambdaQueryWrapper<EntryDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EntryDO::getValue, newValue);
        return this.getOne(queryWrapper);
    }

    /**
     * 根据词库id列表，查询词库所有的词条。 若词库ids为空则返回空集合
     *
     * @param wordStockIds
     * @return
     */
    @Override
    public List<EntryDO> listByWordStockIds(List<Long> wordStockIds) {
        if (CollectionUtils.isEmpty(wordStockIds)) {
            return Lists.newArrayList();
        }
        LambdaQueryWrapper<EntryDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(EntryDO::getWordStockId, wordStockIds);
        return this.list(queryWrapper);
    }

    /**
     * 根据词库id、以及词条归一值模糊查询
     *
     * @param wordStockId
     * @param value
     * @return
     */
    @Override
    public List<EntryDO> listByWordStockIdAndValue(Long wordStockId, String value) {
        if (Objects.isNull(wordStockId)) {
            return Lists.newArrayList();
        }
        LambdaQueryWrapper<EntryDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EntryDO::getWordStockId, wordStockId)
                .and(!StringUtils.isEmpty(value), wapper -> {
                    wapper.like(!StringUtils.isEmpty(value), EntryDO::getValue, value)
                            .or()
                            .like(!StringUtils.isEmpty(value), EntryDO::getSynonyms, value);
                });
        queryWrapper.orderByDesc(EntryDO::getCreatedAt);
        return this.list(queryWrapper);
    }

    @Override
    public Boolean physicalDeleteByWordStockIds(List<Long> wordStockIds) {
        if (CollectionUtils.isEmpty(wordStockIds)) {
            return Boolean.FALSE;
        }
        return this.baseMapper.physicalDeleteByWordStockIds(wordStockIds);
    }

    @Override
    public List<EntryDO> queryByWordStockIds(List<Long> wordStockIds) {
        if (CollectionUtils.isEmpty(wordStockIds)) {
            return Lists.newArrayList();
        }
        LambdaQueryWrapper<EntryDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(EntryDO::getWordStockId, wordStockIds);
        return this.list(queryWrapper);
    }
}




